package com.sixth.controller;


import com.sixth.bean.bo.RoleListBO;
import com.sixth.bean.bo.RolePermissionBO;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.RolePermissionVO;
import com.sixth.constant.Constant;
import com.sixth.service.AdminRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("admin/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;

    //@RequiresPermissions(value = {"admin:role:options"},logical = Logical.OR)
    @RequestMapping("options")
    public BaseRespVo options(){
        BasePageInfoVo basePageInfoVo = adminRoleService.options();


        return BaseRespVo.ok(basePageInfoVo);
    }

    @RequiresPermissions(value = {"admin:role:list"},logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list(RoleListBO roleList){
        BasePageInfoVo basePageInfoVo = adminRoleService.list(roleList);

        if(basePageInfoVo == null){
            return BaseRespVo.invalidData("没有数据");
        }
        return BaseRespVo.ok(basePageInfoVo);
    }

    @RequiresPermissions(value = {"admin:role:permissions"},logical = Logical.OR)
    @RequestMapping(value = "permissions",method = RequestMethod.GET)
    public BaseRespVo permissions(Integer roleId){

        RolePermissionVO permissions = adminRoleService.permissions(roleId);

        return BaseRespVo.ok(permissions);

    }

    @RequiresPermissions(value = {"admin:role:permissions"},logical = Logical.OR)
    @RequestMapping(value = "permissions",method = RequestMethod.POST)
    public BaseRespVo permissions(@RequestBody RolePermissionBO rolePermissionBO){
        if(rolePermissionBO.getRoleId() == 1){
            return BaseRespVo.ok("超级管理员不可修改");
        }

        int code = adminRoleService.permissions(rolePermissionBO);

        if(code == Constant.NON){
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok();

    }

}
